package com.mzj.saas.rent;

import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.core.BaseController;
import com.haixiaoke.saas.repository.core.page.TableDataInfo;
import com.haixiaoke.saas.repository.exception.BizException;
import com.haixiaoke.saas.repository.pojo.ApartmentChargeItem;
import com.haixiaoke.saas.repository.service.IApartmentChargeItemService;
import com.mzj.saas.annotation.CheckAuth;
import com.mzj.saas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公寓收费项目Controller
 *
 * @author qixi
 * @date 2023-06-13
 */
@RestController
@RequestMapping("/business/chargeItem")
public class ApartmentChargeItemController extends BaseController {
    @Autowired
    private IApartmentChargeItemService apartmentChargeItemService;
    @Autowired
    private LoginUserUtil loginUserUtil;

    /**
     * 获取公寓收费项目详细信息
     */
    @GetMapping(value = "/{itemId}")
    @CheckAuth
    public AjaxResult getInfo(@PathVariable("itemId") Long itemId) {
        return success(apartmentChargeItemService.selectApartmentChargeItemByItemId(itemId));
    }

    /**
     * 获取收费项目列表
     * @param apartmentChargeItem
     * @return
     */
    @GetMapping("/list")
    @CheckAuth
    public TableDataInfo list(ApartmentChargeItem apartmentChargeItem) {
        startPage();
        Long orgId = loginUserUtil.getOrgId();
        apartmentChargeItem.setOrgId(orgId);
        List<ApartmentChargeItem> list = apartmentChargeItemService.selectApartmentChargeItemByOrgId(orgId);
        return getDataTable(list);
    }

    @PostMapping
    @CheckAuth
    public AjaxResult add(@RequestBody  ApartmentChargeItem apartmentChargeItem) {
        //填写创建人
        Long orgId = loginUserUtil.getOrgId();
        String id = loginUserUtil.getId();
        apartmentChargeItem.setOrgId(orgId);
        apartmentChargeItem.setCreateBy(id);
        String itemName = apartmentChargeItem.getItemName();
        if (!apartmentChargeItemService.checkItemNameUnique(itemName,orgId)){
            throw  new BizException("收费项目名称已存在");
        }
        return toAjax(apartmentChargeItemService.insertApartmentChargeItem(apartmentChargeItem));
    }

    /**
     * 修改公寓收费项目
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ApartmentChargeItem apartmentChargeItem) {
        String id = loginUserUtil.getId();
        apartmentChargeItem.setUpdateBy(id);
        return toAjax(apartmentChargeItemService.updateApartmentChargeItem(apartmentChargeItem));
    }

    /**
     * 删除公寓收费项目
     */
    @DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable Long[] itemIds) {
        return toAjax(apartmentChargeItemService.deleteApartmentChargeItemByItemIds(itemIds));
    }

}
